package com.edisoncgh.controller;

import com.edisoncgh.pojo.Book;
import com.edisoncgh.pojo.User;
import com.edisoncgh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    // controller调service层
    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;

    // 查询全部书籍，返回一个用户展示页面
    @RequestMapping("/allUser")
    public String list(Model model) {
        List<User> list = userService.findAllUser();
        model.addAttribute("list", list);
        return "allUser";
    }

    // 跳转到新增用户页面
    @RequestMapping("/toAddUser")
    public String toAddPaper() {
        return "addUser";
    }

    // 新增用户的请求
    @RequestMapping("/addUser")
    public String addPaper(User user) {
        userService.addUser(user);
        return "redirect:/user/allUser";
    }

    // 跳转到修改页面
    @RequestMapping("/toUpdate")
    public  String toUpdatePage(Integer id,Model model) {
        User user = userService.findUserById(id);
        model.addAttribute("QUser", user);
        return "updateUser";
    }

    // 修改用户
    @RequestMapping("/updateUser")
    public String updateBook(User user) {
        boolean feedback = userService.updateUser(user);
        if (feedback) {
            System.out.println("添加成功:"+user);
        }
        return "redirect:/user/allUser";
    }

    // 删除用户
    @RequestMapping("/deleteUser")
    public String deleteBook(Integer id) {
        userService.deleteUserById(id);
        return "redirect:/user/allUser";
    }
}
